<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Session;
use Cache;

class OrderController extends Controller
{
	
	// 订单列表
	public function index( Request $request )
	{
		// 获取路由
		$route_array = request()->route()->getAction();
		$controller = explode("\\",$route_array['controller']);
		$result = explode("@",$controller[3]);
		// 控制器 	$result[0];
		// 方法 	$result[1];
		$auth_final = '';
		foreach( Session::get('auths') as $key=> $auth_info )
		{
			// 符合条件
			if( $auth_info['controller']==$result[0] && $auth_info['func']==$result[1] )
			{
				$auth_final = true;
				break;
			}else
			{
				$auth_final = false;
			}
		} 
		// 权限验证失败
		if( !$auth_final )
		{
			return back()->with( ['authinfo'=>'没有此项权限'] );
		}
		// 获取 搜索关键字
		$keyword = $request->input('keyword',"");
		if( empty($keyword) )
		{
			// 为空 说明没有搜索
			$one = 'o.id';
			$two = '!=';
			$three = '0';
		}else
		{
			// 订单编号 
			$one = 'o.id';
			$two = '=';
			$three = $keyword;
		}
		
		// 订单列表		
		$orders = DB::table('order as o')
				-> select( 'o.id','o.create_at','o.need_to_pay',DB::raw( " case when o.type=1 then '散客' else 'vip' end as typeInfo " ),'u.phone','o.type','o.status' )
				-> leftJoin( 'user as u','u.id','=','o.user_id' )
				-> where( $one,$two,$three ) 
				-> orderBy( 'o.create_at','desc' )
				// 每页10条
				-> paginate(10);
		$details = array();
		foreach( $orders as $key => $order )
		{
			$detail = DB::table('order_detail')
				-> select( 'name','pic_url','num','price' )
				-> where( 'oid',$order->id )
				-> get();
			$details[$key] = $detail;
		}
		return view('order.index',[ 'orders'=>$orders,'details'=>$details,'request'=>$keyword ]);
			
		
	}
	
	
	// 取消订单
	public function cancle( Request $request )
	{
		$data = $request -> except('_token');
		// 订单id
		$oid = isset( $data['oid'] ) ? $data['oid'] : '' ;
		if( empty($oid) )
		{
			$this -> ajAxReturn('订单id不能为空');
		}
		$order = DB::table('order')
			-> where( 'id',$oid )
			-> first();
		if( empty($order) )
		{
			$this -> ajAxReturn('订单信息不存在');
		}
		switch( $order->type )
		{
			case '1':
				// 散客
				// 取消订单
				$res = DB::table('order')
					-> where( 'id',$oid )
					-> update( array('status'=>2) );
				if( !$res )
				{
					$this -> ajAxReturn('取消失败，刷新页面重试');
				}
				$this -> ajAxReturn('取消成功');
			break;
			default:
				// vip
				// 查询会员
				$user = DB::table('user')
					-> where( 'id',$order->user_id )
					-> first();
				if( empty($user) )
				{
					$this -> ajAxReturn('订单会员信息不存在');
				}
				$huifu = $user->money + $order->need_to_pay;
				// 恢复账户余额
				$res = DB::table('user')
					-> where( 'id',$order->user_id )
					-> update( array('money'=>$huifu) );
				if( !$res )
				{
					$this -> ajAxReturn('恢复会员余额失败，刷新页面重试');
				}
				// 取消订单
				DB::table('order')
					-> where( 'id',$oid )
					-> update( array('status'=>2) );
				$this -> ajAxReturn('取消成功,已恢复会员余额');
			break;
		}
	}
	
	
	// 经营数据
	public function sheet()
	{
		// 获取路由
		$route_array = request()->route()->getAction();
		$controller = explode("\\",$route_array['controller']);
		$result = explode("@",$controller[3]);
		// 控制器 	$result[0];
		// 方法 	$result[1];
		$auth_final = '';
		foreach( Session::get('auths') as $key=> $auth_info )
		{
			// 符合条件
			if( $auth_info['controller']==$result[0] && $auth_info['func']==$result[1] )
			{
				$auth_final = true;
				break;
			}else
			{
				$auth_final = false;
			}
		} 
		// 权限验证失败
		if( !$auth_final )
		{
			return back()->with( ['authinfo'=>'没有此项权限'] );
		}
		// 今日成交订单量
		$today_count = DB::table('order')
				-> where( 'create_at','>=',date('Y-m-d') )
				// 成交
				-> where( 'status',1 )
				-> count();
		// 今日成交营业额
		$today_money = DB::table('order')
				-> where( 'create_at','>=',date('Y-m-d') )
				// 正常
				-> where( 'status',1 )
				-> sum('need_to_pay');
				
		// 当月成交订单量
		$month_count = DB::table('order')
				-> where( 'create_at','>=',date('Y-m-01') )
				// 成交
				-> where( 'status',1 )
				-> count();
		// 当月成交营业额
		$month_money = DB::table('order')
				-> where( 'create_at','>=',date('Y-m-01') )
				// 成交
				-> where( 'status',1 )
				-> sum('need_to_pay');
				
		// 当月取消订单量
		$cancle_count = DB::table('order')
				-> where( 'create_at','>=',date('Y-m-01') )
				// 取消
				-> where( 'status',2 )
				-> count();
		// 当月取消营业额
		$cancle_money = DB::table('order')
				-> where( 'create_at','>=',date('Y-m-01') )
				// 取消
				-> where( 'status',2 )
				-> sum('need_to_pay');
				
		$data = array(
			'today'=>array(
				array(
				'desc'=>date('m-d').' 成交量',
				'num'=>$today_count
				),
				array(
					'desc'=>date('m-d').' 交易额',
					'num'=>$today_money
				)
			),
			'month'=>array(
				array(
				'desc'=>date('m').' 月成交量',
				'num'=>$month_count
				),
				array(
					'desc'=>date('m').' 月交易额',
					'num'=>$month_money
				)
			),
			'cancle_month'=>array(
				array(
				'desc'=>date('m').' 月取消订单量',
				'num'=>$cancle_count 
				),
				array(
					'desc'=>date('m').' 月取消营业额',
					'num'=>$cancle_money
				)
			),
			
		);
		return view('order.sheet',[ 'data'=>$data ]);
	
	}
	
	
	// 查询一段时期的经营数据
	public function search( Request $request )
	{
		// 包含查询日期
		$data = $request -> except('_token');
		$date = isset($data['date']) ? $data['date'] : '';
		if( empty($date) )
		{
			$this -> ajAxReturn('日期不能为空');
		}
		$arr = explode( ' - ',$date );
		if( count($arr)!=2 )
		{
			$this -> ajAxReturn('日期格式有问题');
		}
		$start = $arr[0];
		$end = $arr[1];
		// 这段时期成交订单量
		$count = DB::table('order')
				-> where( 'create_at','>=',$start )
				-> where( 'create_at','<=',$end )
				// 成交
				-> where( 'status',1 )
				-> count();
		
		// 这段时期成交营业额
		$sum_money = DB::table('order')
				-> where( 'create_at','>=',$start )
				-> where( 'create_at','<=',$end )
				// 正常
				-> where( 'status',1 )
				-> sum('need_to_pay');
		$return = array(
			'deal'=>array(
				array(
				'desc'=>'成交量',
				'num'=>$count
				),
				array(
					'desc'=>'交易额',
					'num'=>$sum_money
				)
			),
		);
		$html ="";
		foreach( $return as $key => $info )
		{
			foreach( $info as $k=>$in )
			{
				$html .= "<div style='float:left;margin-left:3%;margin-top:20px;width:180px;height:80px;background-color:orange;border-radius:95px;'>";
					$html .= "<div style='float:left;width:180px;height:39px;font-size:29px;text-align:center;color:white;margin-top:10px;' >";
					$html .= $in['num'];
					$html .= "</div>";
					$html .= "<div style='float:left;width:180px;height:35px;font-size:15px;text-align:center;color:white;' >";
					$html .= $in['desc'];
					$html .= "</div>";
				$html .= "</div>";
			}
		}
		exit( $html );
	}
	
	// 执行 生成订单
	public function orderAdd( Request $request )
	{
		$data = $request -> except('_token');
		// 订单类型
		$type = isset($data['type']) ? $data['type'] : '';
		// 订单内商品
		$order_sku = isset($data['order_sku']) ? $data['order_sku'] : array();
		if( count($order_sku)<1 )
		{
			$this -> ajAxReturn('<span style="color:red;">订单内没有商品</span>');
		}
		$need_to_pay = isset($data['need_to_pay']) ? $data['need_to_pay'] : '';
		if( empty($need_to_pay) )
		{
			$this -> ajAxReturn('<span style="color:red;">订单总价不能为空</span>');
		}
		switch( $type )
		{
			case 'price':
				// 散客结算
				$order = array(
					// 特殊用户id=1 为散客
					'user_id' => 1,
					// 1散客订单 2vip订单
					'type'=>1,
					'need_to_pay'=>$need_to_pay,
					'create_at'=>date('Y-m-d H:i:s')
				);
				// 返回订单id
				$oid = DB::table('order')->insertGetId( $order );
				if( $oid )
				{
					foreach( $order_sku as $key => $sku )
					{
						$detail = array(
							'oid' => $oid,
							'sku' => $sku['sku_id'],
							'name' => $sku['sku_name'],
							'pic_url' => $sku['sku_pic'],
							'num' => $sku['num'],
							'price' => $sku['sku_price'],
							'xiaoji' => $sku['num']*$sku['sku_price'],
						);
						// 订单详情
						DB::table('order_detail')->insertGetId( $detail );
					}
					$this -> ajAxReturn('散客订单提交成功');
				}
				$this -> ajAxReturn('<span style="color:red;">订单提交失败</span>');
			break;
			case 'vip':
				// 会员结算
				$phone = isset($data['phone']) ? $data['phone'] : '';
				if( empty($phone) ) 
				{
					$this -> ajAxReturn('<span style="color:red;">会员手机号码为空</span>');
				}
				// 会员信息
				$user_info = DB::table('user')
						-> where( 'phone',$phone )
						-> first();
				if( empty($user_info) )
				{
					// 不是会员
					$this -> ajAxReturn( '<span style="color:red;">该号码不是会员</span>' );
				}
				$money = $user_info->money-$need_to_pay;
				if( $money<0 )
				{
					// 余额不足
					$this -> ajAxReturn( '<span style="color:red;">余额不足</span>' );
				}
				
				// 1.修改会员余额
				DB::table('user')
					-> where( 'phone',$phone )
					-> update( array('money'=>$money ) );
				// 2.订单主表
				$order = array(
					'user_id' => $user_info->id,
					// 1散客订单 2vip订单
					'type'=>2,
					'need_to_pay'=>$need_to_pay,
					'create_at'=>date('Y-m-d H:i:s')
				);
				// 返回订单id
				$oid = DB::table('order')->insertGetId( $order );
				if( $oid )
				{
					foreach( $order_sku as $key => $sku )
					{
						$detail = array(
							'oid' => $oid,
							'sku' => $sku['sku_id'],
							'name' => $sku['sku_name'],
							'pic_url' => $sku['sku_pic'],
							'num' => $sku['num'],
							'price' => $sku['sku_price'],
							'xiaoji' => $sku['num']*$sku['sku_price'],
						);
						// 订单详情
						DB::table('order_detail')->insertGetId( $detail );
					}
					$this -> ajAxReturn('会员订单提交成功');
				}
				// 3.订单详情表
				$this -> ajAxReturn('<span style="color:red;">订单提交失败</span>');
			break;
			default:
				$this -> ajAxReturn('订单类型错误');
			break;
		}
		
	}
	
	
}
